<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <!-- TODO 图片还有待替换 -->
        <img src="../assets/logo2.png" alt="haha" height="40px" width="40px"/>
        <span>《大数据技术与体系》教学辅助系统</span>
      </div>
      <div class="home-class">
        <!-- <span class="diClass">第</span>   -->
        <el-select 
          placeholder="请选择年级"
          v-model="userLevel"
          @change="changeLevel"
          >
          <el-option v-for="item in userLevelEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>
        <!-- <span class="jiClass">级</span> -->
      </div>
      <el-button type="info" size="mini" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="$route.path">
          <!-- 一级菜单 -->
          <el-submenu :index="String(item.id)" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="item.icon"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <!-- item.path 不带斜线就是跳转到/home/users -->
            <!-- 带斜线就是跳转到/users -->
            <el-menu-item :index="item.path" v-for="item in item.children" :key="item.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主题 -->
      <el-main><router-view :mainLevel="mainLevel"/></el-main>
    </el-container>
  </el-container>
</template>

<script>
import loginApi from '@/api/login'
import { mapGetters, mapState, mapActions } from 'vuex'
export default {
  data() {
    return {
      isCollapse: false,
      userLevel: 2018,
      mainLevel: 2018
    }
  },
  created() {
    this.initChapter(this.userLevel)
  },
  methods: {
    changeLevel() {
      this.initChapter(this.userLevel)
      this.mainLevel = this.userLevel
    },
    logout() {
      let _this = this
      loginApi.logout().then(function (result) {
        if (result && result.code === _this.ok.code) {
          _this.$message.success(result.message)
        }
        window.sessionStorage.clear()
        _this.$router.replace('/login')
      })
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    ...mapActions('chapter', { initChapter: 'initChapter' }),
  },
  computed: {
    ...mapState('resCode', {
      ok: state => state.OK
    }),
    ...mapState('enumItem', {
      userLevelEnum: state => state.user.userLevelEnum,
    }),
    ...mapGetters([
      'menuList'
    ])
  }
}
</script>

<style lang="less" scoped>
// .diClass {
//   margin-left: 0px !important;
//   margin-right: 15px;
//   margin-bottom: 5px;
//   font-size: 28px;
// }
// .jiClass {
//   font-size: 28px;
// }
.home-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0%;
  align-items: center;
  color: #fff;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
  .home-class {
    /deep/.el-select {
    width: 150px;
    .el-input {
      .el-input__inner {
      background-color: #373d41 !important;
      color: #fff !important;
      border: none !important;
      font-size: 28px;
    }
    } 
  }
  }
  
}
.el-aside {
  background: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
